import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
import net.miginfocom.swing.*;
/*
 * Created by JFormDesigner on Sun Dec 08 18:13:22 CST 2024
 */



/**
 * @author jjchen
 */
public class MF_bookmanage_addnewbook extends JFrame {
    public MF_bookmanage_addnewbook() {
        initComponents();
    }

    private void button1MouseClicked(MouseEvent e) {
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        bookManagement mb = new bookManagement();
        mb.setVisible(true);
    }

    private void button1MouseClicked2(MouseEvent e) {
        String a = textField1.getText();
        String b = textField2.getText();
        String c = textField3.getText();

        // 获取JSpinner的值并转换为Date
        Date date = (Date) spinner1.getValue();

        // 使用Calendar提取年、月、日
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始，所以加1
        int day = calendar.get(Calendar.DAY_OF_MONTH);


        // 将年、月、日转换为字符串
        String d = year + "-" + month + "-" + day ; // 格式化为"yyyy-MM-dd"

        String f = textField5.getText();
        String g = textField6.getText();

        if (a.isEmpty() || b.isEmpty() || c.isEmpty() || d.isEmpty() || f.isEmpty()|| g.isEmpty()) {
            JOptionPane.showMessageDialog(this, "添加失败请填写完整。");
            return; // 如果有字段为空，不继续执行后面的代码
        }
        String url = "jdbc:mysql://localhost:3306/javaproject";
        String username = "root";
        String rootPassword = "root"; // 注意：不要在生产环境中使用硬编码的密码

        String sql = "INSERT INTO book_information VALUES (?, ?, ?, ?, ?, ?);";

        try (Connection conn = DriverManager.getConnection(url, username, rootPassword);
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, a);
            stmt.setString(2, b);
            stmt.setString(3, c);
            stmt.setString(4, d); // 使用格式化后的日期字符串
            stmt.setString(5, f);
            stmt.setString(6, g);

            int count = stmt.executeUpdate();
            System.out.println(count);
            JOptionPane.showMessageDialog(this, "添加图书成功！");
        } catch (SQLException ex) {
            // 处理SQL异常，例如记录日志或显示错误消息
            ex.printStackTrace();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label3 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        label7 = new JLabel();
        textField3 = new JTextField();
        label5 = new JLabel();
        spinner1 = new JSpinner();
        label6 = new JLabel();
        textField5 = new JTextField();
        label8 = new JLabel();
        textField6 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setTitle("C:\\workspace");
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[13,fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[15,left]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- label1 ----
        label1.setText("\u4e66\u7c4d\u4fe1\u606f\u6dfb\u52a0");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getStyle() | Font.BOLD, label1.getFont().getSize() + 3f));
        contentPane.add(label1, "cell 8 1 1 2");

        //---- label3 ----
        label3.setText("\u4e66\u7c4did");
        contentPane.add(label3, "cell 2 4 4 1");

        //---- label2 ----
        label2.setText("\u540d\u79f0");
        contentPane.add(label2, "cell 2 5 4 1");
        contentPane.add(textField1, "cell 5 4 9 1");
        contentPane.add(textField2, "cell 5 5 9 1");

        //---- label7 ----
        label7.setText("\u4f5c\u8005");
        contentPane.add(label7, "cell 2 6");
        contentPane.add(textField3, "cell 5 6 9 1");

        //---- label5 ----
        label5.setText("\u5165\u5e93\u65f6\u95f4");
        contentPane.add(label5, "cell 2 7");

        //---- spinner1 ----
        spinner1.setModel(new SpinnerDateModel(new java.util.Date((System.currentTimeMillis()/60000)*60000), null, null, java.util.Calendar.MONTH));
        contentPane.add(spinner1, "cell 5 7 9 1");

        //---- label6 ----
        label6.setText("\u6570\u91cf");
        contentPane.add(label6, "cell 2 8");
        contentPane.add(textField5, "cell 5 8 9 1");

        //---- label8 ----
        label8.setText("\u4f4d\u7f6e");
        contentPane.add(label8, "cell 2 9");
        contentPane.add(textField6, "cell 5 9 9 1");

        //---- button1 ----
        button1.setText("\u6dfb\u52a0");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
                button1MouseClicked2(e);
            }
        });
        contentPane.add(button1, "cell 7 10 3 1");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    public static void main(String[] args) {
        MF_bookmanage_addnewbook MF = new MF_bookmanage_addnewbook();
        MF.setVisible(true);
        MF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label3;
    private JLabel label2;
    private JTextField textField1;
    private JTextField textField2;
    private JLabel label7;
    private JTextField textField3;
    private JLabel label5;
    private JSpinner spinner1;
    private JLabel label6;
    private JTextField textField5;
    private JLabel label8;
    private JTextField textField6;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
